

. .\Lib.Strings.ps1
. .\Lib.MimeType.ps1
. .\Lib.WebPage.ps1


#====================================================================
#
#		SQL SERVER ADMINISTRATION WEB APPLICATION
#
#====================================================================


#--------------------------------------------------------------------
Function Get-SqlDmoConnection
{
	[psobject] $SqlServer = new-object -comobject "SQLDMO.SQLServer"
	$SqlServer.LoginSecure = [bool]1
	[void] $SqlServer.Connect( $ServerName )
	return $SqlServer
}


#--------------------------------------------------------------------
Function Get-HtmlServerInfo
{
	param
	(
		  [psobject] $WebRequest
		, [psobject] $SqlServer
	)
# Begin Html -->
@"
	<h3>Server Information</h3>
	<table>
		<tr>
			<td><b>Server Name</b></td>
			<td>$( $SqlServer.NetName )</td>
		</tr>
		<tr>
			<td><b>Server Version</b></td>
			<td>$( $SqlServer.VersionString.Replace( "`n", "<br />" ) )</td>
		</tr>
		<tr>
			<td><b>Logged in as</b></td>
			<td>$( $SqlServer.Login )</td>
		</tr>
	</table>
"@
# <-- End Html
	return
}


#--------------------------------------------------------------------
Function Get-HtmlServerLogins
{
	param
	(
		  [psobject] $WebRequest
		, [psobject] $SqlServer
	)
# Begin Html -->
@"
	<h3>Server Logins</h3>
	<table>
		<tr>
			<th>Login Name</th>
			<th>Database</th>
		</tr>
"@
# <-- End Html
	[string] $url = $WebRequest.Protocol + "://" + $WebRequest.WebServer.ServerAddress
	[psobject] $Login = $null
	foreach( $Login in $SqlServer.Logins )
	{
		[string] $ItemUrl = $url + "/logins/" + $Login.Name.Replace( "\", "%5C" )
# Begin Html -->
@"
		<tr>
			<td><a href="$ItemUrl">$( $Login.Name )</a></td>
			<td>$( $Login.Database )</td>
		</tr>
"@
# <-- End Html
	}
# Begin Html -->
@"
	</table>
"@
# <-- End Html
	return
}


#--------------------------------------------------------------------
Function Get-HtmlServerDatabases
{
	param
	(
		  [psobject] $WebRequest
		, [psobject] $SqlServer
	)

# Begin Html -->
@"
	<h3>Server Databases</h3>
	<table>
		<tr>
			<th>Database Name</th>
			<th>Owner</th>
			<th>Created</th>
			<th>Size</th>
			<th>Users</th>
		</tr>
"@
# <-- End Html

	[string] $url = $WebRequest.Protocol + "://" + $WebRequest.WebServer.ServerAddress
	[psobject] $Database = $null
	foreach( $Database in $SqlServer.Databases )
	{
		[string] $ItemUrl = $url + "/databases/" + $Database.Name
		if( $Database.SystemObject -eq $false )
		{
# Begin Html -->
@"
			<tr>
				<td><a href="$ItemUrl">$( $Database.Name )</a></td>
				<td>$( $Database.Owner )</td>
				<td>$( $Database.CreateDate )</td>
				<td>$( $Database.Size ) MB</td>
"@
# <-- End Html
			[psobject] $User = $null
			[string] $html = ""
			foreach( $User in $Database.Users )
			{
				if( $html -ne "" )
					{ $html += ", " }
				$html += "<a href="""
				$html += $url + "/users/" + $User.Name
				$html += """>$( $User.Name )</a>"
			}
# Begin Html -->
@"
				<td>$html</td>
			</tr>
"@
# <-- End Html
		}
	}

# Begin Html -->
@"
	</table>
"@
# <-- End Html

	return
}


#--------------------------------------------------------------------
Function Get-HtmlDatabaseInformation
{
	param
	(
		  [psobject]	$WebRequest
		, [psobject]	$SqlServer
		, [string]		$DatabaseName
	)
	[psobject] $Database = $SqlServer.Databases.Item( $DatabaseName )
# Begin Html -->
@"
	<h3>Database Information</h3>
	<table>
		<tr>
			<td><b>Database Name</b></td>
			<td>$( $Database.Name )</td>
		</tr>
		<tr>
			<td><b>Owner</b></td>
			<td>$( $Database.Owner )</td>
		</tr>
		<tr>
			<td><b>Date Created</b></td>
			<td>$( $Database.CreateDate )</td>
		</tr>
		<tr>
			<td><b>Allocated Size</b></td>
			<td>$( $Database.Size ) MB</td>
		</tr>
		<tr>
			<td><b>Data Size</b></td>
			<td>$( $Database.DataSpaceUsage ) KB</td>
		</tr>
		<tr>
			<td><b>Index Size</b></td>
			<td>$( $Database.IndexSpaceUsage ) KB</td>
		</tr>
		<tr>
			<td><b>Space Available</b></td>
			<td>$( $Database.SpaceAvailableInMB ) MB</td>
		</tr>
	</table>
"@
# <-- End Html
	return
}


#--------------------------------------------------------------------
Function Get-HtmlDatabaseTables
{
	param
	(
		  [psobject]	$WebRequest
		, [psobject]	$SqlServer
		, [string]		$DatabaseName
	)
	[psobject] $Database = $SqlServer.Databases.Item( $DatabaseName )
# Begin Html -->
@"
	<h3>Database Tables</h3>
	<table>
		<tr>
			<th>Table Name</th>
			<th>Owner</th>
			<th>Date Created</th>
			<th>Row Count</th>
			<th>Data Size</th>
			<th>Index Size</th>
		</tr>
"@
# <-- End Html

	[psobject] $Table = $null
	foreach( $Table in $Database.Tables )
	{
		if( $Table.SystemObject -eq $false )
		{
# Begin Html -->
@"
			<tr>
				<td>$( $Table.Name )</td>
				<td>$( $Table.Owner )</td>
				<td>$( $Table.CreateDate )</td>
				<td>$( $Table.Rows )</td>
				<td>$( $Table.DataSpaceUsed )</td>
				<td>$( $Table.IndexSpaceUsed )</td>
			</tr>
"@
# <-- End Html
		}
	}

# Begin Html -->
@"
	</table>
"@
# <-- End Html
	return
}


#--------------------------------------------------------------------
Function Process-Web-GET
{
	param
	(
		  [psobject] $WebRequest
	)

	[psobject] $WebResponse = New-WebResponse
	$WebResponse.WebRequest		= $WebRequest
	$WebResponse.Resource		= $null
	$WebResponse.ResponseCode	= "501 Not Implemented"
	
Write-Host "GET -> $( $WebRequest.ResourceName )"
	[string] $html = ""

	# Build the web page.
	[string] $html = Get-HtmlHeader $WebRequest "PowerShell WebApp: SqlAdmin"
	$html += "<h3>PowerShell WebApp: SqlAdmin</h3>"
	$html += Get-HtmlNavigationMenu $WebRequest
	$html += "<br />"
	[psobject] $SqlServer = Get-SqlDmoConnection

	if( $WebRequest.ResourceName -eq "" )
	{
		# Root Directory
		$html += Get-HtmlServerInfo $WebRequest $SqlServer
		$html += Get-HtmlServerLogins $WebRequest $SqlServer
		$html += Get-HtmlServerDatabases $WebRequest $SqlServer
	}
	elseif( $WebRequest.ResourceName -eq "logins" )
	{
		# Login Listing
		$html += Get-HtmlServerLogins $WebRequest $SqlServer
	}
	elseif( $WebRequest.ResourceName -eq "databases" )
	{
		# Database Listing
		$html += Get-HtmlServerDatabases $WebRequest $SqlServer
	}
	elseif( $WebRequest.ResourceName -like "databases\*" )
	{
		# Database Detail
		[string] $DatabaseName = $WebRequest.ResourceName.Substring( 10 )
		$html += Get-HtmlDatabaseInformation $WebRequest $SqlServer $DatabaseName
		$html += Get-HtmlDatabaseTables $WebRequest $SqlServer $DatabaseName
	}

	$html += Get-HtmlFooter $WebRequest

	# Construct the response.
	$WebResponse.Resource = New-Resource
	$WebResponse.Resource.Root = ""
	$WebResponse.Resource.Path = $WebRequest.ResourceName
	$WebResponse.Resource.Name = ""
	$WebResponse.Resource.Mime = "text/html"
	$WebResponse.Resource.Data = AsciiString2Bytes( $html )
	$WebResponse.Resource.Size = $WebResponse.Resource.Data.Length
	$WebResponse.ResponseCode = "200 OK"

	# Send the response header.
Write-Host $WebResponse.ResponseCode
	[bool] $b = Send-WebResponseHeader $WebResponse
	if( $b -eq $false ) { return $false }

	# Send the resource content.
	if( $WebResponse.Resource -ne $null )
	{
		#Write-Debug -Message $Resource.Data
	    $cbSent = $Socket.Send( $WebResponse.Resource.Data )
	    if ( $cbSent -eq -1 ) { return $false }
    }
	return $true

}

